import { storeBindingsBehavior } from "mobx-miniprogram-bindings";
import cart from "../../../../store/cart";

Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [
    {
      store: cart,
      fields: ["cartList"],
      actions: ["addCart", "reduceCart"],
    },
  ],
  properties: {
    goods: Object,
    showSpecDialog: Object,
    isOpenTime: Boolean,
  },
  data: {
    num: 0,
    proList: [],
  },
  observers: {
    cartList: function (cartList) {
      let num = 0;
      let proList = [];
      for (let item of cartList) {
        if (item.id === this.data.goods.id) {
          num = num + item.count;
          proList.push(item);
        }
      }
      this.setData({ num: num, proList });
    },
  },
  methods: {
    handleMinus(e) {
      let op = e.currentTarget.dataset.op;
      if (op === "multi-spec") {
        this.handleOpSpec("reduce");
      }
      for (let i = 0; i < this.data.num - Number(e.detail); i++) {
        if (op === "not-multi-spec") {
          this.handleOp("reduce");
        }
      }
    },
    handlePlus(e) {
      let op = e.currentTarget.dataset.op;
      if (op === "multi-spec") {
        this.handleOpSpec("add");
      }
      for (let i = 0; i < Number(e.detail) - this.data.num; i++) {
        if (op === "not-multi-spec") {
          this.handleOp("add");
        }
      }
    },
    handleOp(op) {
      // let op = e.currentTarget.dataset.op
      let { name, price, id, attrList, imageUrl, ruleList, specList } =
        this.data.goods;
      let selectAttrList = [];
      if (attrList && attrList.length > 0) {
        for (let item of attrList) {
          selectAttrList.push(item.values[0]);
        }
      }
      let item = {
        name,
        price,
        id,
        spec: specList && specList.length > 0 ? specList[0].name : "",
        attrList:
          selectAttrList && selectAttrList.length > 0 ? selectAttrList : [],
        imageUrl,
        ruleList: ruleList && ruleList.length > 0 ? ruleList : [],
      };
      if (op === "add") {
        this.addCart(item);
      } else {
        this.reduceCart(item);
      }
    },
    handleOpSpec(op) {
      let { num, proList } = this.data;
      // let op = e.currentTarget.dataset.op;
      let flag = false;

      if (op === "add") {
        this.handleShowSpecDialog();
      } else {
        let { name, price, id, attrList, imageUrl, ruleList, specList } =
          this.data.goods;
        let selectAttrList = [];
        if (attrList && attrList.length > 0) {
          for (let item of attrList) {
            selectAttrList.push(item.values[0]);
          }
        }
        // const specIndex=this.goods.specList.findIndex(v=>v.name===)
        let item = {
          name,
          price,
          id,
          spec: specList && specList.length > 0 ? specList[0].name : "",
          attrList:
            selectAttrList && selectAttrList.length > 0 ? selectAttrList : [],
          imageUrl,
          ruleList: ruleList && ruleList.length > 0 ? ruleList : [],
        };
        if (num <= 1) {
          let { attrList } = proList[0];
          let newAttr = [];
          attrList.forEach((element) => {
            newAttr.push(element);
          });
          item.attrList = newAttr;
          item.spec = proList[0].spec;
          this.reduceCart(item);
        } else {
          let { attrList } = proList[0];
          item.spec = proList[0].spec;
          let newAttr = [];
          attrList.forEach((element) => {
            newAttr.push(element);
          });

          for (var i = proList.length - 1; i >= 0; i--) {
            var targetNode = proList[i];
            for (var j = 0; j <= i; j++) {
              if (targetNode.remark !== proList[j].remark) {
                flag = true;
              }
            }
          }
          if (flag === true) {
            wx.showToast({
              title: "请前往购物车删减商品数量",
              icon: "none",
              duration: 2000,
            });
          } else {
            item.attrList = newAttr;
            this.reduceCart(item);
          }
        }
      }
    },
    handleShowImg(e) {
      let imgUrl = e.currentTarget.dataset.img;
      wx.previewImage({
        current: "",
        urls: [imgUrl],
      });
    },
    handleShowSpecDialog() {
      // 规格选择组件在父级页面
      this.triggerEvent("selectspec", { goods: this.data.goods });
    },
  },
});
